<?php

namespace app\controller\api\controller;

use app\service\ReportService;
use app\support\ResponseHelper;
use DI\Attribute\Inject;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use support\Log;
use Webman\Http\Request;
use Webman\Http\Response;

/**
 * 数据统计相关接口
 */
#[RestController("/api/report")]
class ReportController
{
    #[Inject]
    protected ReportService $reportService;

    /**
     * 营业额统计
     * @param Request $request
     * @return Response
     */
    #[GetMapping("/turnoverStatistics")]
    public function turnoverStatistics(Request $request): Response
    {
        $begin = $request->get('begin');
        $end = $request->get('end');
        
        if (!$begin || !$end) {
            return ResponseHelper::failJson('开始日期和结束日期不能为空');
        }
        
        Log::info("营业额数据统计：{$begin},{$end}");
        
        $result = $this->reportService->getTurnoverStatistics($begin, $end);
        return ResponseHelper::successJson($result->toArray());
    }

    /**
     * 用户统计
     * @param Request $request
     * @return Response
     */
    #[GetMapping("/userStatistics")]
    public function userStatistics(Request $request): Response
    {
        $begin = $request->get('begin');
        $end = $request->get('end');
        
        if (!$begin || !$end) {
            return ResponseHelper::failJson('开始日期和结束日期不能为空');
        }
        
        Log::info("用户数据统计：{$begin},{$end}");
        
        $result = $this->reportService->getUserStatistics($begin, $end);
        return ResponseHelper::successJson($result->toArray());
    }

    /**
     * 订单统计
     * @param Request $request
     * @return Response
     */
    #[GetMapping("/ordersStatistics")]
    public function ordersStatistics(Request $request): Response
    {
        $begin = $request->get('begin');
        $end = $request->get('end');
        
        if (!$begin || !$end) {
            return ResponseHelper::failJson('开始日期和结束日期不能为空');
        }
        
        Log::info("订单数据统计：{$begin},{$end}");
        
        $result = $this->reportService->getOrderStatistics($begin, $end);
        return ResponseHelper::successJson($result->toArray());
    }

    /**
     * 销量排名top10
     * @param Request $request
     * @return Response
     */
    #[GetMapping("/top10")]
    public function top10(Request $request): Response
    {
        $begin = $request->get('begin');
        $end = $request->get('end');
        
        if (!$begin || !$end) {
            return ResponseHelper::failJson('开始日期和结束日期不能为空');
        }
        
        Log::info("销量排名top10：{$begin},{$end}");
        
        $result = $this->reportService->getSalesTop10($begin, $end);
        return ResponseHelper::successJson($result->toArray());
    }

    /**
     * 导出运营数据报表
     * @return Response
     */
    #[GetMapping("/export")]
    public function export(): Response
    {
        Log::info("导出运营数据报表");
        
        $result = $this->reportService->exportBusinessData();
        return ResponseHelper::successJson($result);
    }
}